/*
 * 解析从数据库读到的数据
 * 返回ArrayList<NewPayRecord>
 * 内存储int格式时间及double格式balance
 */

package control;

import java.sql.ResultSet;
import java.util.ArrayList;

import model.NewPayRecord;
import model.TotalRecord;

import dao.NewPayRecordDAO;
import dao.TotalRecordDAO;
import exception.ViewException;

public class Parser {

	public static ArrayList<TotalRecord> getTotalRecord(int quarySelect) throws ViewException{
		ResultSet rs = TotalRecordDAO.getTotalRecord(quarySelect);
		ArrayList<TotalRecord> recordList = new ArrayList<TotalRecord>();
		try {
			while (rs.next()) {
				TotalRecord record = new TotalRecord();

				String[] date = getDateNoSplit(rs.getString(1));

				record.setYear(Integer.valueOf(date[0]));
				record.setMonth(Integer.valueOf(date[1]));
				record.setDate(Integer.valueOf(date[2]));

				record.setBalanceValue(Double.valueOf(rs.getString(2)));

				recordList.add(record);
			}
		} catch (Exception e) {

			e.printStackTrace();

		}
		return recordList;
	}

	public static ArrayList<NewPayRecord> getNewPayRecord(String quaryID) throws ViewException{
		ResultSet rs = NewPayRecordDAO.getNewPayRecord(quaryID);

		ArrayList<NewPayRecord> recordList = new ArrayList<NewPayRecord>();
		try {
			while (rs.next()) {
				NewPayRecord record = new NewPayRecord();

				String[] date = getDateNoSplit(rs.getString(1));
				String[] time = getTimeNoSplit(rs.getString(2));

				record.setYear(Integer.valueOf(date[0]));
				record.setMonth(Integer.valueOf(date[1]));
				record.setDate(Integer.valueOf(date[2]));

				record.setHour(Integer.valueOf(time[0]));
				record.setMinute(Integer.valueOf(time[1]));
				record.setSecond(Integer.valueOf(time[2]));

				record.setBalanceValue(Double.valueOf(rs.getString(3)));

				recordList.add(record);
			}
		} catch (Exception e) {

			e.printStackTrace();

		}
		return recordList;
	}

	// 按位解析时间字符
	private static String[] getDateNoSplit(String s) {

		String[] ss = new String[10];

		ss[0] = s.substring(0, 4);

		ss[1] = s.substring(4, 6);

		ss[2] = s.substring(6, 8);

		return ss;

	}

	// 按位取时间，区分小时是否两位
	private static String[] getTimeNoSplit(String s) {

		String[] ss = new String[10];

		if (s.startsWith(" ")) {

			ss[0] = "00";

			ss[1] = "00";

			ss[2] = "00";

		} else if (Integer.valueOf(s) > 95959) {

			ss[0] = s.substring(0, 2);

			ss[1] = s.substring(2, 4);

			ss[2] = s.substring(4, 6);

		} else if (Integer.valueOf(s) > 1) {

			ss[0] = s.substring(0, 1);

			ss[1] = s.substring(1, 3);

			ss[2] = s.substring(3, 5);

		}

		return ss;

	}

	// 按分隔符-解析时间字符
	private static String[] getDate(String s) {

		String[] ss = new String[40];

		for (int i = 0; i < ss.length; i++) {

			ss = s.split("-");


		}

		return ss;
	}

	public static  ArrayList<NewPayRecord>  getViewRecord(String viewName, String cardID) throws ViewException{
		
		ResultSet rs = NewPayRecordDAO.getViewRecord(viewName, cardID);

		
		ArrayList<NewPayRecord> recordList = new ArrayList<NewPayRecord>();
		
		try {
			while (rs.next()) {
				
				NewPayRecord record = new NewPayRecord();

				String[] date = getDateNoSplit(rs.getString(1));
				
				record.setYear(Integer.valueOf(date[0]));
				record.setMonth(Integer.valueOf(date[1]));
				record.setDate(Integer.valueOf(date[2]));

				record.setBalanceValue(Double.valueOf(rs.getString(2)));

				recordList.add(record);
			}
		} catch (Exception e) {

			e.printStackTrace();

		}
		return recordList;
	}
	
	//获取标准表的记录，mark:"recharge,pay,query,other"
	public static  ArrayList<NewPayRecord>  getTableRecord(String tableName,String mark, String cardID) throws ViewException{
		
		ResultSet rs = NewPayRecordDAO.getNetworkPayRecord(tableName,mark, cardID);

		
		ArrayList<NewPayRecord> recordList = new ArrayList<NewPayRecord>();
		
		try {
			while (rs.next()) {
				
				NewPayRecord record = new NewPayRecord();

				String[] date = getDateNoSplit(rs.getString(1));
				
				record.setYear(Integer.valueOf(date[0]));
				record.setMonth(Integer.valueOf(date[1]));
				record.setDate(Integer.valueOf(date[2]));

				record.setBalanceValue(Double.valueOf(rs.getString(2)));

				recordList.add(record);
			}
		} catch (Exception e) {

			e.printStackTrace();

		}
		return recordList;
	}
}
